import { _decorator, BoxCollider, Component, ITriggerEvent, Node } from 'cc';
import { Constand } from './Constant';
import { GameManager } from './GameManager';
import { AuidoManager } from './AuidoManager';
import { PoolMananger } from './PoolMananger';
import { EnemyPlane } from './EnemyPlane';
import { Bullet } from './Bullet';
import { UiManager } from './UiManager';
const { ccclass, property } = _decorator;

@ccclass('SelPlane')
export class SelPlane extends Component implements IDamage {
    @property
    public damage: number = 50;
    onEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.on('onTriggerEnter', this._onTriggerEnter, this);
    }

    onDisable(): void {
        const collider = this.getComponent(BoxCollider);
        collider.off('onTriggerEnter', this._onTriggerEnter, this);
    }

    private _onTriggerEnter(event: ITriggerEvent) {
        let damage = 0;
        const grop = event.otherCollider.getGroup();
        if (grop == Constand.CollistionType.ENEMY) {
            const com = event.otherCollider.getComponent(EnemyPlane);
            damage = com.damage;
        } else if (grop == Constand.CollistionType.E_BULLET) {
            const com = event.otherCollider.getComponent(Bullet);
            damage = com.damage;
        }
        //被击中，扣血
        GameManager.getInstance().hpCurr -= damage;
        if (GameManager.getInstance().hpCurr < 0) {
            GameManager.getInstance().hpCurr = 0;
        }
        UiManager.getInstance().updateHp();
        if (GameManager.getInstance().hpCurr == 0) {
            //播放爆炸动画
            GameManager.getInstance().createExplode(this.node.position, 1);
            GameManager.getInstance().gameOver();
            AuidoManager.getInstance().play("player");
        }
    }

}


